import numpy as np
from scipy.linalg import qr
'''
4. 设计一个方法，可以随机生成n × n的正交矩阵（非对角阵）
通过对n x n矩阵进行QR因式分解，得到一个随机n x n正交矩阵Q
'''


def getQ(dim):
    H = np.random.randn(dim, dim)
    if np.linalg.det(H) == 0:
        print("This matrix is singular, cannot be inversed!")

    print(H)
    Q, R = qr(H)
    return Q


n = 3
Q = getQ(3)
np.set_printoptions(suppress=True)
print(Q.dot(Q.T))
